@extends('Admin.base')
@section('css')
@endsection
@section('content')
    <div class="layui-card">
        <div class="layui-card-header layuiadmin-card-header-auto">
            <fieldset class="layui-elem-field layui-field-title">
                <legend>新建优惠券</legend>
            </fieldset>
        </div>
        <div class="layui-card-body">
            <form class="layui-form" id="layui-form" method="post">
                {{csrf_field()}}

                <div class="layui-form-item layui-col-md6">
                    <label for="" class="layui-form-label">优惠券名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="activity_name" style="width: 280px;" value="" lay-verify="required"  class="layui-input" >
                    </div>
                </div>

                <div class="layui-form-item  ">
                    <div class="layui-inline">
                        <label for=""  class="layui-form-label">优惠类型</label>
                        <div class="layui-input-block">
                            <input type="radio" lay-filter="radio"  name="rule_type"  value="1" title="立减"  >
                            <input type="radio" lay-filter="radio"  name="rule_type"  value="2" title="满减" checked="checked" >
                        </div>
                    </div>
                </div>
                <div class="layui-form-item layui-form-text count1">
                    <label for="" class="layui-form-label"> </label>
                    <div class="layui-input-block">
                        <div class="layui-inline count">
                            <div class="layui-form-mid">满</div>
                            <div class="layui-input-inline" style="width: 100px;">
                                <input type="text" onkeyup="value=value.replace(/^(0+)|[^\d]+/g,'')" name="full"  maxlength="4" {{ isset( $data) ? 'disabled' :''}} placeholder="￥" value="{{ $data->full ?? '' }}" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <div class="layui-form-mid">减</div>
                            <div class="layui-input-inline" style="width: 100px;">
                                <input type="text" onkeyup="value=value.replace(/^(0+)|[^\d]+/g,'')" maxlength="4"  name="less" lay-verify="required number" {{ isset( $data) ? 'disabled' :''}} placeholder="￥" value="{{ $data->less ?? '' }}" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item layui-col-md6">
                    <label class="layui-form-label">有 效 期</label>
                    <div class="layui-inline">
                        <div class="layui-form-mid">领券后 </div>
                        <div class="layui-input-inline" style="width: 100px;">
                            <input type="text"  maxlength="5" name="activity_time"  onkeyup="value=value.replace(/^(0[0-9]+)|[^\d]+/g,'0')"  lay-verify="required number"  autocomplete="off" class="layui-input">

                        </div>
                        <div class="layui-form-mid"  >天</div>
                        <div class="layui-form-mid content layui-word-aux" style="width: 100%;">填0表示永久生效</div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        {{isset($data) ? method_field('PUT') :  method_field('post')}}
                        {{csrf_field()}}
                        <button  class="layui-btn closew"  type="button" >取消</button>
                        <button type="button" class="layui-btn"lay-submit lay-filter="login">创建</button>
                    </div>
                </div>

            </form>
        </div>
    </div>
@endsection
@section('script')
    <script src="/js/ajax.js"></script>
    <script>
        layui.use(['laydate', 'form'], function () {
            var form = layui.form;
            $(".closew").on('click',function () {
                parent.layer.closeAll();
            });
            if ($("input[name='rule_type']:checked").val() == 2)  {
                $(".count").show();
            }else{
                $(".count").hide()
            };
            form.on('submit(login)', function(data){

                var rule_type = $("input[name='rule_type']:checked").val(),
                    full = $("input[name='full']").val() ,
                    less =  $("input[name='less']").val(),
                    activity_time = $("input[name='activity_time']").val();


                if( rule_type == 2 && full == ''){
                    return layer.msg('满多少必填');
                }
                if (less == ''){
                    return layer.msg('减多少必填');
                }
                if ( rule_type == 2 && (parseInt( full) < parseInt(less)) ){
                    return layer.msg('满多少请输入大于等于减的数字');
                }
                if ( activity_time == '' ){
                    return layer.msg('有效期必填');
                }
//                return false;
                $.ajax({
                    url:"{{url('/admin/issue/activity_store')}}",
                    type:'post',
                    data:data.field,
                    dataType:'JSON',
                    success:function(res){

                        if(res.code =='200'){
                            layer.msg(res.message,{time:2000},function () {
                                parent.layui.table.reload('LAY-table');
                                parent.layer.closeAll();
                            });
                        } else{
                            layer.msg(res.message);
                        }

                    },
                    error:function (data) {

                    }
                }) ;

                return false;
            });
            form.on('radio(radio)', function (data) {
                if (data.value == 2)  {
                    $(".count").show();
                }else{
                    $(".count").hide()
                }
                form.render();
            });
            form.render();
        });

    </script>
@endsection